// packageA/pages/profile/profile.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {
            name: '用户2048'
        },
        showPopup: false,
        navBarHeight: 0,
        tabBarHeight: 0,
        contentHeight: 0,
        navHeight: wx.getStorageSync('navHeight') ? wx.getStorageSync('navHeight') : 88,
        // 悬浮球相关数据
        floatBall: {
            x: 300, // 初始x坐标
            y: 500, // 初始y坐标
            isDragging: false
        },
        screenWidth: 0,
        screenHeight: 0,
        templateList: [
            {
                id: 1,
                url: 'https://image.shengxuankj.com/fronts/obligation_icon.png',
                text: '待付款'
            },
            {
                id: 2,
                url: 'https://image.shengxuankj.com/fronts/available_for_use_icon.png',
                text: '待使用'
            },
            {
                id: 3,
                url: 'https://image.shengxuankj.com/fronts/finished_icon.png',
                text: '已完成'
            },
            {
                id: 4,
                url: 'https://image.shengxuankj.com/fronts/refund_icon.png',
                text: '退款/取消'
            },
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserInfo()

        // 计算剩余可视高度
        this.calculateContentHeight();
        
        // 获取屏幕尺寸
        const systemInfo = wx.getSystemInfoSync();
        this.setData({
            screenWidth: systemInfo.windowWidth,
            screenHeight: systemInfo.windowHeight
        });
    },

    /**
     * 获取用户信息
     */
    getUserInfo() {
        // 模拟获取用户信息
        const userInfo = wx.getStorageSync('userInfo')
        if (userInfo) {
            this.setData({
                userInfo: userInfo
            })
        }
    },
    /**
   * 计算主要内容区域的高度
   */
    calculateContentHeight() {
        const systemInfo = wx.getSystemInfoSync();
        const windowHeight = systemInfo.windowHeight;
        const safeAreaBottom = systemInfo.safeArea ? systemInfo.screenHeight - systemInfo.safeArea.bottom : 0;
        const tabBarHeight = 50 + safeAreaBottom; // 自定义tabbar高度(100rpx转px约50px) + 安全区域
        
        // 首页导航栏不占用空间，所以内容高度就是窗口高度减去tabbar高度
        const contentHeight = windowHeight;
        
        this.setData({
            tabBarHeight: tabBarHeight,
            contentHeight: contentHeight
        });
    },
    /**
     * 编辑个人信息
     */
    editProfile() {
        this.setData({
            showPopup: true
        })
    },

    /**
     * 关闭弹窗
     */
    onClosePopup() {
        this.setData({
            showPopup: false
        })
    },

    /**
     * 保存个人信息
     */
    saveProfile() {
        wx.showToast({
            title: '保存成功',
            icon: 'success'
        })
        this.setData({
            showPopup: false
        })
    },

    /**
     * 返回上一页
     */
    goBack() {
        wx.navigateBack()
    },

    // 悬浮球触摸开始
    onFloatBallTouchStart(e) {
        this.setData({
            'floatBall.isDragging': true
        });
    },

    // 悬浮球拖拽移动
    onFloatBallTouchMove(e) {
        if (!this.data.floatBall.isDragging) return;
        
        const touch = e.touches[0];
        this.setData({
            'floatBall.x': touch.clientX - 30, // 减去悬浮球半径
            'floatBall.y': touch.clientY - 30
        });
    },

    // 悬浮球触摸结束
    onFloatBallTouchEnd(e) {
        const { floatBall, screenWidth } = this.data;
        const touch = e.changedTouches[0];
        const centerX = screenWidth / 2;
        
        let targetX;
        if (touch.clientX < centerX) {
            // 拖到左半边，吸附到左边
            targetX = 10;
        } else {
            // 拖到右半边，吸附到右边
            targetX = screenWidth - 70; // 减去悬浮球宽度
        }
        
        this.setData({
            'floatBall.x': targetX,
            'floatBall.y': touch.clientY - 30,
            'floatBall.isDragging': false
        });
    },

    goToOrderPage(){
        wx.navigateTo({
          url: '/packageA/order/order',
        })
    }
})